package org.javaymw.springboot.service;

import org.javaymw.springboot.base.enums.ErrorEnum;

/**
 * 结果
 * Created by zhaoxinguo on 2018/1/19.
 */
public class Result<D> {

    /**
     * 是否成功
     */
    private Boolean success;

    /**
     * 错误码
     */
    private String errorCode;

    /**
     * 错误信息
     */
    private String errorMsg;

    /**
     * 数据
     */
    private D data;

    /**
     * 构造成功结果
     *
     * @return
     */
    static Result success() {
        return success(null);
    }

    /**
     * 构造成功结果
     *
     * @param data
     * @return
     */
    static Result success(Object data) {
        Result result = new Result();
        result.success(true);
        result.setData(data);
        return result;
    }

    /**
     * 构造错误结果
     *
     * @param errorEnum
     * @return
     */
    static Result error(ErrorEnum errorEnum) {
        Result result = new Result();
        result.success(false);
        result.setErrorCode(errorEnum.code());
        result.setErrorMsg(errorEnum.msg());
        return result;
    }

    /**
     * 检查是否为指定错误
     *
     * @param errorEnum
     * @return
     */
    public boolean check(ErrorEnum errorEnum) {
        return errorEnum.code().equals(this.errorCode);
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public D getData() {
        return data;
    }

    public void setData(D data) {
        this.data = data;
    }
}
